package com.wyd.jwpkglxt_ssm.service.impl;

import com.wyd.jwpkglxt_ssm.entity.PtDepartment;
import com.wyd.jwpkglxt_ssm.dao.PtDepartmentDao;
import com.wyd.jwpkglxt_ssm.service.PtDepartmentService;
import org.springframework.stereotype.Service;

import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * (PtDepartment)表服务实现类
 *
 * @author wyd
 * @since 2021-08-11 10:38:22
 */
@Service("ptDepartmentService")
public class PtDepartmentServiceImpl implements PtDepartmentService {
    @Autowired
    private PtDepartmentDao ptDepartmentDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public PtDepartment queryById(Integer id) {
        return this.ptDepartmentDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<PtDepartment> queryAllByLimit(int offset, int limit) {
        return this.ptDepartmentDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param ptDepartment 实例对象
     * @return 实例对象
     */
    @Override
    public PtDepartment insert(PtDepartment ptDepartment) {
        this.ptDepartmentDao.insert(ptDepartment);
        return ptDepartment;
    }

    /**
     * 修改数据
     *
     * @param ptDepartment 实例对象
     * @return 实例对象
     */
    @Override
    public PtDepartment update(PtDepartment ptDepartment) {
        this.ptDepartmentDao.update(ptDepartment);
        return this.queryById(ptDepartment.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.ptDepartmentDao.deleteById(id) > 0;
    }

    @Override
    public Boolean deletePtDepartment(String ids) {
        if(ids.contains("-")){
            String[] idsplit = ids.split("-");
            List<Integer> ptDepartmentIds = new ArrayList<>();
            for(String s : idsplit){
                ptDepartmentIds.add(Integer.parseInt(s));
            }
            int delProfessionalTitles = this.ptDepartmentDao.deletePtDepartmentBatch(ptDepartmentIds);
            if(delProfessionalTitles!=0){
                return true;
            }
        }else if(!"".equals(ids)){
            Integer id = Integer.parseInt(ids);
            int delProfessionalTitle = this.ptDepartmentDao.deleteById(id);
            if(delProfessionalTitle!=0){
                return true;
            }
        }

        return false;
    }
}